var submitUrl = "http://orkut-scope.appspot.com/migrate/import";

function submitDetails(x)
{
    document.forms[x].enterTime.value = Date.parse(document.forms[x].enterTime.value.replace("*", "+"))
    document.forms[x].exitTime.value = Date.parse(document.forms[x].exitTime.value.replace("*", "+"))
    if (document.forms[x].enterTime.value !== "NaN") 
    {
        if (document.forms[x].exitTime.value === "NaN") 
        {
            document.forms[x].exitTime.value = document.forms[x].enterTime.value;
        }
        var submitFrame = document.createElement("iframe");
        submitFrame.name = "dummy" + x;
        document.forms[x].appendChild(submitFrame);
        document.forms[x].action = submitUrl;
        document.forms[x].target = submitFrame.name
        document.forms[x].owner.value = transformPerson(eval(document.forms[x].owner.value))
        document.forms[x].viewer.value = transformPerson(eval(document.forms[x].viewer.value))
        
        eval("viewerIdJSON = " + document.forms[x].viewer.value)
        if (viewerIdJSON != null && typeof(viewerIdJSON.id) !== "undefined") 
        {
            viewerIdJSON = "orkut-" + viewerIdJSON.id;
        }
        else 
        {
            viewerIdJSON = "null";
        }
        document.forms[x].viewerId.value = viewerIdJSON
        
        eval("ownerJSON = " + document.forms[x].owner.value)
        if (typeof(ownerJSON.id) !== "undefined") 
        {
            document.forms[x].submit();
        }
    }
}

var recordNumber = 0
var handle = window.setInterval(function()
{
    if (recordNumber > document.forms.length) 
    {
        window.clearInterval(handle);
        window.open(window.location)
		window.close();
        return;
    }
    try 
    {
        submitDetails(recordNumber++);
    } 
    catch (e) 
    {
    
    }
}, 3000);

function transformPerson(jsonString)
{
    var owner = null;
    try 
    {
        eval("ownerJSON = " + jsonString)
        owner = 
        {
            "id": ownerJSON.obj_.Id,
            "name": 
            {
                "fields_": 
                {
                    "familyName": ownerJSON.obj_.FullName.FamilyName,
                    "givenName": ownerJSON.obj_.FullName.GivenName,
                }
            },
            "profileUrl": "#",
            "thumbnailUrl": ownerJSON.obj_.Thumbnail,
            "urls": [
            {
                "fields_": 
                {
                    "value": "#",
                    "type": "profile",
                    "address": "#"
                }
            }]
        };
        owner = YAHOO.lang.JSON.stringify(owner);
    } 
    catch (e) 
    {
        owner = jsonString;
    }
    return owner;
}
